package com.example.czy.utils;

import com.example.czy.enums.ExceptionEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * 响应信息主体
 *
 * @param <T>
 * @author czy
 */
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
@Data
public class R<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    private Integer code;
    private String msg;
    private T data;

    public static <T> R<T> ok() {
        return restResult(null, ExceptionEnum.SUCCESS);
    }

    public static <T> R<T> ok(T data) {
        return restResult(data, ExceptionEnum.SUCCESS);
    }

    public static <T> R<T> ok(T data, String msg) {
        return restResult(data, ExceptionEnum.SUCCESS.getCode(), msg);
    }

    public static <T> R<T> failed() {
        return restResult(null, ExceptionEnum.ERROR);
    }

    public static <T> R<T> failed(String msg) {
        return restResult(null, ExceptionEnum.ERROR.getCode(), msg);
    }

    public static <T> R<T> failed(T data) {
        return restResult(data, ExceptionEnum.ERROR);
    }

    public static <T> R<T> failed(T data, String msg) {
        return restResult(data, ExceptionEnum.ERROR.getCode(), msg);
    }

    public static <T> R<T> failed(Integer code, String msg) {
        return restResult(null, code, msg);
    }

    public static <T> R<T> restResult(T data, ExceptionEnum exceptionEnum) {
        return restResult(data, exceptionEnum.getCode(), exceptionEnum.getMsg());
    }

    public static <T> R<T> restResult(T data, Integer code, String msg) {
        return new R<>(code, msg, data);
    }

}
